package com.example.common.interceptor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@EnableWebMvc
@Configuration
@Component
public class WebConfig implements WebMvcConfigurer {
    @Resource
    private GlobalInterceptor globalInterceptor;

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        ObjectMapper mapper = new ObjectMapper();
        SimpleModule module = new SimpleModule();
        /**
         * 解决后端Long传给前端精度丢失问题
         */
        module.addSerializer(Long.class, ToStringSerializer.instance);
        module.addSerializer(Long.TYPE, ToStringSerializer.instance);
        /**
         * 解决 LocalDateTime 传给前端不支持问题
         */
        module.addSerializer(LocalDateTime.class, ToStringSerializer.instance);

        mapper.registerModule(module);
        converter.setObjectMapper(mapper);
        converters.add(converter);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //添加拦截器
        InterceptorRegistration ir = registry.addInterceptor(globalInterceptor);
        /**
         * 配置拦截规则
         */
        ir.addPathPatterns("/**");
    }
}
